package business;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.ListIterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDAO;
import orm.Users;

import sun.text.CompactShortArray.Iterator;
import util.DBHelper;
import util.Utility;

public class UserInfoServlet extends HttpServlet {
	
	/**
	 * TeacherInfoServlet
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public UserInfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String strFunc = request.getParameter("func");
		int PageSize = Utility.PageSize;
		Connection con = null;
		Statement stmt = null;
		
		if (strFunc.equals("load")) {
			UserDAO userDAO = new UserDAO();
			try {
				request.setAttribute("pageNo", 1);
				ArrayList<Users> al = userDAO.getAllUsers();
				int aaa = al.size();
				if (aaa % PageSize == 0) {
					request.setAttribute("totalPage",aaa/PageSize);
				} else {
					request.setAttribute("totalPage",(aaa/PageSize) + 1);
				}
				
				al = new ArrayList<Users>();
				ListIterator<Users> it = userDAO.getAllUsers().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					Users t = it.next();
					Users t1 = new Users();
						
					t1.setUserID(t.getUserID());
					t1.setRealName(t.getRealName());
					t1.setPosition(t.getPosition());
					t1.setAuthorizationDate(t.getAuthorizationDate());
					
					al.add(t1);
					ii++;
					if(ii > (PageSize-1)) {
						break;
					}
				}
				
				request.setAttribute("userList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_UserInfo).forward(request, response);
		} else if (strFunc.equals("edit")) {
			int userID = Integer.parseInt(request.getParameter("userID"));
			
			UserDAO userDAO = new UserDAO();
			try {
				Users user = userDAO.getUserID(userID);
				request.setAttribute("user", user);
				request.getRequestDispatcher("../" + Utility.JSP_UserEdit).forward(request, response);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (strFunc.equals("delete")) {
			int userID = Integer.parseInt(request.getParameter("userID"));
			try {
				con = DBHelper.getConnection();
				con.setAutoCommit(false);
				stmt = con.createStatement();
				
				UserDAO userDAO = new UserDAO();
				Users user = userDAO.getUserID(userID);

				userDAO.deleteUser(stmt, userID);
				
				con.commit();
				stmt.close();
				ArrayList al = null;
				al = new ArrayList<Users>();
				ListIterator<Users> it = userDAO.getAllUsers().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					Users t = it.next();
					Users t1 = new Users();
						
					t1.setUserID(t.getUserID());
					t1.setRealName(t.getRealName());
					t1.setPosition(t.getPosition());
					t1.setAuthorizationDate(t.getAuthorizationDate());
					
					al.add(t1);
					ii++;
					if(ii > (PageSize-1) ) {
						break;
					}
				}
	
				String tmp = request.getParameter("HDPageNo");
				int nPageNo = Integer.parseInt(tmp); 
				ArrayList<Users> alUser = userDAO.getAllUsers();
				int totalPage = 0;
				if (alUser.size() % PageSize == 0) {
					totalPage = alUser.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = alUser.size()/PageSize + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				
				request.setAttribute("pageNo", 1);
				
				request.setAttribute("userList", al);
				
				request.getRequestDispatcher("../" + Utility.JSP_UserInfo).forward(request, response);
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
				try {
					con.rollback();
				} catch (SQLException ee) {
					ee.printStackTrace();
				}
			}
		} else if (strFunc.equals("paging")) {
		
			String strSub = request.getParameter("sub");
			String tmp = request.getParameter("HDPageNo");
			int nPageNo = Integer.parseInt(tmp); 
			
			UserDAO userDAO = new UserDAO();
			try {
//				request.setAttribute("pageNo", nPageNo);
				ArrayList<Users> al = userDAO.getAllUsers();
				int totalPage = 0;
				if (al.size() % PageSize == 0) {
					totalPage = al.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = (al.size()/PageSize) + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				if (strSub == null)
				{
					
				} else if (strSub.equals("start")) {
					nPageNo = 1;
				} else if (strSub.equals("front")) {
					nPageNo--;
				} else if (strSub.equals("next")) {
					nPageNo++;
				} else if (strSub.equals("last")) {
					nPageNo = totalPage ;
				} 
				request.setAttribute("pageNo", nPageNo);
				al = new ArrayList<Users>();
				ListIterator<Users> it = userDAO.getAllUsers().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					ii++;
					Users t = it.next();
					if (ii <= PageSize * (nPageNo - 1))
						continue;
					
					Users t1 = new Users();
					t1.setUserID(t.getUserID());
					t1.setRealName(t.getRealName());
					t1.setPosition(t.getPosition());
					t1.setAuthorizationDate(t.getAuthorizationDate());
					
					al.add(t1);
					
					if(ii > PageSize * nPageNo - 1 ) {
						break;
					}
				}
				
				request.setAttribute("userList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_UserInfo).forward(request, response);
		} else if (strFunc.equals("searchPage")) {
			
			String strSub = request.getParameter("sub");
			String tmp = request.getParameter("PageNo");
			int nPageNo = Integer.parseInt(tmp);

			UserDAO userDAO = new UserDAO();
			try {
//				request.setAttribute("pageNo", nPageNo);
				ArrayList<Users> al = userDAO.getAllUsers();
				int totalPage = 0;
				if (al.size() % PageSize == 0) {
					totalPage = al.size()/PageSize;
					request.setAttribute("totalPage",totalPage);
				} else {
					totalPage = al.size()/PageSize + 1;
					request.setAttribute("totalPage",totalPage);
				}
				
				request.setAttribute("pageNo", nPageNo);
				al = new ArrayList<Users>();
				ListIterator<Users> it = userDAO.getAllUsers().listIterator();
				int ii = 0;
				while (it.hasNext()) {
					ii++;
					Users t = it.next();
					if (ii <= PageSize * (nPageNo - 1))
						continue;
					
					Users t1 = new Users();
					t1.setUserID(t.getUserID());
					t1.setRealName(t.getRealName());
					t1.setPosition(t.getPosition());
					t1.setAuthorizationDate(t.getAuthorizationDate());
					
					al.add(t1);
					
					if(ii > PageSize * nPageNo - 1 ) {
						break;
					}
				}
				
				request.setAttribute("userList", al);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("../" + Utility.JSP_UserInfo).forward(request, response);
		}
 	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
